﻿#include "addfrienddialog.h"
#include "ui_addfrienddialog.h"
#include "utils.h"
#include "clientdisktcpsocket.h"
#include <QIcon>
#include <QMessageBox>
#include <Qmutex>

AddFriendDialog::AddFriendDialog(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::AddFriendDialog)
{
    ui->setupUi(this);
    lb_usrName = new QLabel("用户名");
    lb_avatar = new QLabel("头像");
    lb_Msg = new QLabel("用户账号：");
    pbn_addFriend = new QPushButton("添加好友");
    m_pGridLayout = new QGridLayout(ui->widget);
    // 0 行 0列开始占 2行2列
    m_pGridLayout->addWidget(lb_avatar,0,0,2,1);
    m_pGridLayout->addWidget(lb_usrName,0,1,1,2,Qt::AlignLeft | Qt::AlignVCenter);
    m_pGridLayout->addWidget(lb_Msg,1,1,1,2, Qt::AlignLeft | Qt::AlignVCenter);
    m_pGridLayout->addWidget(pbn_addFriend,0,3,2,1,Qt::AlignRight | Qt::AlignVCenter);

    lb_avatar->setMaximumSize(50,50);
    lb_usrName->setFont((QFont("Microsoft YaHei", 10, 70)));
    // lb_Msg->setFont((QFont("Microsoft YaHei", 9, 50)));
    lb_Msg->setStyleSheet("QLabel{color:rgb(150,150,154)}");
    utils::setRoundRectImg(lb_avatar,":/img/avatar.png",10);

    this->setStyleSheet("QWidget#AddFriendDialog{"
                        "background-color:white;"
                        "}"
                        );
    this->setWindowIcon(QIcon(":/img/Search.png"));
    this->setWindowTitle("添加好友");
    // 隐藏widget
    isShowWidget(false);

    connect(pbn_addFriend,&QPushButton::clicked, this, &AddFriendDialog::slot_pbn_addFriend_clicked);


}


AddFriendDialog::~AddFriendDialog()
{
    delete ui;
}

AddFriendDialog* AddFriendDialog::getInstance()
{
    static QMutex mutex;
    static QScopedPointer<AddFriendDialog> inst;
    if (Q_UNLIKELY(!inst)) {
        mutex.lock();
        if (!inst) {
            inst.reset(new AddFriendDialog);
        }
        mutex.unlock();
    }
    return inst.data();
}

void AddFriendDialog::on_pbn_Search_clicked()
{
    friendUsrName = ui->lineEdit->text();
    if(friendUsrName.isEmpty()){
        isShowWidget(false);
        return ;
    }
    PDU* pdu = mkPDU(0);
    pdu->uiMsgType = ENUM_MSG_TYPE_SEARCH_USER_REQUEST;
    strcpy_s(pdu->caData,friendUsrName.toStdString().c_str());
    ClientDiskTcpSocket::getInstance()->write((char*)pdu,pdu->uiPDULen);
    MYLOG<<"用户搜索名为: "<<pdu->caData;
    free(pdu);
    pdu = NULL;
}

void AddFriendDialog::slot_pbn_addFriend_clicked()
{
    PDU *pdu = mkPDU(0);
    pdu->uiMsgType = ENUM_MSG_TYPE_ADD_FRIEND_REQUEST;
    memcpy(pdu->caData,ClientDiskTcpSocket::getInstance()->usrName().toStdString().c_str(),32);
    memcpy(pdu->caData+32,friendUsrName.toStdString().c_str(),32);
    ClientDiskTcpSocket::getInstance()->write((char *)pdu,pdu->uiPDULen);
}

void AddFriendDialog::isShowWidget(bool isShow)
{
    ui->widget->setVisible(isShow);
    this->adjustSize();  // 自动调整窗口大小
}

void AddFriendDialog::showEvent(QShowEvent *event)
{
    ui->lineEdit->clear();
    isShowWidget(false);
    QWidget::showEvent(event); // 调用基类的 showEvent 以确保事件正确处理
}

bool AddFriendDialog::event(QEvent *e)
{
    if (QEvent::WindowDeactivate == e->type())
    {
        this->close();
    }
    return QWidget::event(e);
}

void AddFriendDialog::setAvatar(QString imgPath, int radius)
{
    utils::setRoundRectImg(lb_avatar,imgPath,radius);
}

void AddFriendDialog::setUsrName(QString usrName)
{
    lb_usrName->setText(usrName);
}

void AddFriendDialog::setMsg(QString Msg)
{
    lb_Msg->setText(Msg);
}

void AddFriendDialog::handleSearch(PDU *pduRecv)
{
    MYLOG<<pduRecv->caData;
    if(strcmp(pduRecv->caData,SEARCH_USER_EMPTY) == 0){
        isShowWidget(false);
        QMessageBox::information(this,"搜索",QString("%1: not exist").arg(friendUsrName));
    }else if(strcmp(pduRecv->caData,SEARCH_USER_OFFLINE) == 0){
        isShowWidget(true);
        pbn_addFriend->setEnabled(false);
        setUsrName(friendUsrName);
        setMsg("用户已下线");
    }else if(strcmp(pduRecv->caData,SEARCH_USER_OK) == 0){
        isShowWidget(true);
        pbn_addFriend->setEnabled(true);
        setUsrName(friendUsrName);
        setMsg("用户在线");
    }
    if(friendUsrName == ClientDiskTcpSocket::getInstance()->usrName()){
        pbn_addFriend->setEnabled(false);
    }
}

void AddFriendDialog::handleAddFriendRespond(PDU *pduRecv)
{
    QMessageBox::information(this,"添加好友",pduRecv->caData);
}

